{include file="common/head"/}
<style>
    .layui-breadcrumb > * {
        font-size: 18px;
        font-weight: 300;
    }
</style>
<div style="margin: 15px;" class="fadeInUp animated"  ng-app="hd" ng-controller="ctrl">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>
            <!-- <a href="{:url('list')}"> 素材列表</a> > {$title}  -->
            <span class="layui-breadcrumb">
                <a href="{:url('list')}"><i class="layui-icon layui-icon-upload-circle"></i> 课程列表</a>
                <a><cite>{$title}</cite></a>
            </span>
        </legend>
       
    </fieldset>
    <div class="layui-tab" lay-filter="filter">
        <ul class="layui-tab-title">
            <li class="layui-this" lay-id="one">基础信息</li>
            <li lay-id="two">课时信息</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show" >
                <fieldset class="layui-elem-field">
                    <legend>基础信息</legend>
                    <form class="layui-form layui-form-pane" lay-filter="baseInfo" style="margin: 12px;">
                        <div class="layui-form-item">
                            <label class="layui-form-label">专栏分类</label>
                            <div class="layui-input-inline">
                                <select name="category"  lay-filter="category" id="course_category" lay-verify="required">
                                    <option value="">请选择栏目</option>
                                    {volist name="course_category" id="vo"}
                                    <option value="{$vo.id}" {if $baseInfo.brand_id==$vo.id}selected{/if}>{$vo.title}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-4">
                                <input type="text" name="title" lay-verify="required" value="{{field.title}}" placeholder="{:lang('pleaseEnter')}素材标题" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">封面图片</label>
                            <input type="hidden" name="pic" id="pic" lay-verify="required" value="{{field.pic}}">
                            <div class="layui-input-block">
                                <div class="layui-upload">
                                    <button type="button" class="layui-btn layui-btn-primary" id="adBtn"><i
                                            class="icon icon-upload3"></i>点击上传</button>
                                    <div class="layui-upload-list">
                                        <img class="layui-upload-img" id="adPic">
                                        <p id="demoText"></p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">课程类型</label>
                            <div class="layui-input-inline">
                                <select name="type" lay-filter="type" id="type" lay-verify="required">
                                    <option value="">请选择类型</option>
                                    {volist name="course_type" id="vo"}
                                    <option value="{$key}" {if $baseInfo.type==$key}selected{/if}>{$vo}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">所属品牌</label>
                            <div class="layui-input-inline">
                                <select name="brand_id" lay-verify="required" lay-filter="brnad_id">
                                    <option value="">请选择品牌</option>
                                    {volist name="brand" id="vo"}
                                    <option value="{$vo.id}" {if $baseInfo.brand_id==$vo.id}selected{/if}>{$vo.title}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                       
                        <div class="layui-form-item">
                            <label class="layui-form-label">价格(元)</label>
                            <div class="layui-input-4">
                                <input type="text" name="price" lay-verify="required" value="{{field.price}}"
                                    placeholder="{:lang('pleaseEnter')}价格" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">密码</label>
                            <div class="layui-input-4">
                                <input type="password" name="password" lay-verify="" value=""
                                    placeholder="{:lang('pleaseEnter')}密码" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">文字描述</label>
                            <div class="layui-input-block">
                                <textarea name="content" id="content" class="layui-textarea">{{field.content}}</textarea>
                            </div>
                        </div>
                        <input type="hidden" name="id" id="id" value="{{field.id}}" />
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                                <a href="{:url('list')}" class="layui-btn layui-btn-primary">返回</a>
                            </div>
                        </div>
                    </form>
                </fieldset>
            </div>
           
            <div class="layui-tab-item"> <!-- 课时 -->
                <fieldset class="layui-elem-field">
                    <legend>课时信息</legend>
                    <div class="layui-field-box">
                        <div class="demoTable" >
                            <a class="layui-btn item-add" style="float:right; margin-bottom: 5px;"><i class="fa fa-plus"></i>添加</a>
                        </div>
                        <table class="layui-table" id="list_item" lay-filter="list_item" >
                            <colgroup>
                                <col width="150">
                                <col width="350">
                                <col width="150">
                                <col>
                            </colgroup>
                            <thead>
                                <tr>
                                    <th>课时ID</th>
                                    <th>标题</th>
                                    <th>课时排序</th>
                                    <th>学习次数</th>
                                    <th>添加时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {volist name="itemList" id="vo"}
                                <tr>
                                    <td>{$vo.id}</td>
                                    <td>{$vo.title}</td>
                                    <td>{$vo.sort}</td>
                                    <td>{$vo.hit}</td>
                                    <td>{$vo.create_time}</td>
                                    <td>
                                        <button class="layui-btn layui-btn-xs item-edit" data-itemid="{$vo.id}">编辑</button>
                                        <button class="layui-btn layui-btn-xs layui-btn-danger item-delete" data-itemid="{$vo.id}">删除</button>
                                    </td>
                                </tr>
                                {/volist}
                            </tbody>
                        </table>
                    </div>
                </fieldset>
            </div>
        </div>
    </div>
</div>
{include file="common/foot"/}
<script src="/static/common/js/angular.min.js"></script>
<script src="/static/common/js/jquery.2.1.1.min.js"></script>
<script charset="utf-8" src="/static/ueditor/ueditor.config.js"></script>
<script charset="utf-8" src="/static/ueditor/ueditor.all.js"></script>
<script>
    var ue = UE.getEditor('content', {
        initialFrameWidth: 1000,
        initialFrameHeight: 500,
        autoHeightEnabled: false,
        // maximumWords:30000
    });
</script>
<script>
    var m = angular.module('hd',[]);
    var baseInfoId = 0;
    var tab = '{$tab}';
    m.controller('ctrl',['$scope',function($scope) {
        $scope.field = '{$info|raw}'!='null'?{$info|raw}:{id:'',title:''};
        baseInfoId = $scope.field.id;
        layui.use(['form', 'layer','upload','element','table'], function () {
            var form = layui.form, $ = layui.jquery, upload = layui.upload, element = layui.element, table = layui.table;
            if ($scope.field.pic) {
                adPic.src = $scope.field.pic;
            }

            if(tab){
                element.tabChange('filter', tab);
            }

            // 保存
            element.on('tab(filter)', function(data){
                if(data.index > 0){
                    if(baseInfoId == 0){
                        layer.msg('请先保存基础信息!', { time: 1800, icon: 2 });
                        element.tabChange('filter', 'one');
                    }
                    else{
                        return false;
                    }
                }
            });

            // 保存基本信息
            form.on('submit(submit)', function (data) {
                // 提交到方法 默认为本身
                data.field.id = $scope.field.id;
                data.field.content = ue.getContent();
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("", data.field, function (res) {
                    layer.close(loading);
                    if (res.code > 0) {
                        layer.msg(res.msg, {time: 1800, icon: 1});
                        form.render();
                        baseInfoId = res.id
                        element.tabChange('filter', 'two');
                    } else {
                        layer.msg(res.msg, {time: 1800, icon: 2});
                    }
                });
            });

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#adBtn'
                , url: '{:url("UpFiles/upload")}'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#adPic').attr('src', result); //图片链接（base64）
                    });
                },
                done: function (res) {
                    if (res.code > 0) {
                        $('#pic').val(res.url);
                    } else {
                        //如果上传失败
                        return layer.msg('上传失败');
                    }
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });

            // 添加课时
            $("body").on('click', '.item-add', function () {
                layer.open({
                    type: 2
                    , title: '添加课时'
                    , shade: 0
                    , maxmin: true
                    , area: ['600px', '420px']
                    , content: '/avikcm/course/addItem.html?course_id=' + baseInfoId
                });
            });

            // 编辑课时
            $("body").on('click', '.item-edit', function () {
                var obj = $(this);
                var id = obj.data('itemid');
                layer.open({
                    type: 2
                    , title: '编辑课时'
                    , shade: 0
                    , maxmin: true
                    , area: ['600px', '420px']
                    , content: '/avikcm/course/editItem.html?id=' + id
                });
            });

            // 删除课时
            $("body").on('click', '.item-delete', function () {
                var obj = $(this);
                var id = obj.data('itemid');
                var loading = layer.load(1, { shade: [0.1, '#fff'] });
                $.post("delItem", { id: id }, function (res) {
                    layer.close(loading);
                    if (res.code > 0) {
                        obj.parent().parent().remove();
                        layer.msg(res.msg, { time: 1800, icon: 1 });
                    } else {
                        layer.msg(res.msg, { time: 1800, icon: 2 });
                    }
                });
            });

        });
    }]);
</script>